{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# 3_jugs_mip"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/3_jugs_mip.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/3_jugs_mip.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  3 jugs problem using MIP in Google or-tools.\n",
    "\n",
    "  A.k.a. water jugs problem.\n",
    "\n",
    "  Problem from Taha 'Introduction to Operations Research',\n",
    "  page 245f .\n",
    "\n",
    "  Compare with the CP model:\n",
    "     http://www.hakank.org/google_or_tools/3_jugs_regular\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.linear_solver import pywraplp\n",
    "\n",
    "\n",
    "def main(sol='CBC'):\n",
    "  # Create the solver.\n",
    "  print('Solver: ', sol)\n",
    "  solver = pywraplp.Solver.CreateSolver(sol)\n",
    "  if not solver:\n",
    "    return\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 15\n",
    "  start = 0  # start node\n",
    "  end = 14  # end node\n",
    "  M = 999  # a large number\n",
    "\n",
    "  nodes = [\n",
    "      '8,0,0',  # start\n",
    "      '5,0,3',\n",
    "      '5,3,0',\n",
    "      '2,3,3',\n",
    "      '2,5,1',\n",
    "      '7,0,1',\n",
    "      '7,1,0',\n",
    "      '4,1,3',\n",
    "      '3,5,0',\n",
    "      '3,2,3',\n",
    "      '6,2,0',\n",
    "      '6,0,2',\n",
    "      '1,5,2',\n",
    "      '1,4,3',\n",
    "      '4,4,0'  # goal!\n",
    "  ]\n",
    "\n",
    "  # distance\n",
    "  d = [[M, 1, M, M, M, M, M, M, 1, M, M, M, M, M, M],\n",
    "       [M, M, 1, M, M, M, M, M, M, M, M, M, M, M, M],\n",
    "       [M, M, M, 1, M, M, M, M, 1, M, M, M, M, M, M],\n",
    "       [M, M, M, M, 1, M, M, M, M, M, M, M, M, M, M],\n",
    "       [M, M, M, M, M, 1, M, M, 1, M, M, M, M, M, M],\n",
    "       [M, M, M, M, M, M, 1, M, M, M, M, M, M, M, M],\n",
    "       [M, M, M, M, M, M, M, 1, 1, M, M, M, M, M, M],\n",
    "       [M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1],\n",
    "       [M, M, M, M, M, M, M, M, M, 1, M, M, M, M, M],\n",
    "       [M, 1, M, M, M, M, M, M, M, M, 1, M, M, M, M],\n",
    "       [M, M, M, M, M, M, M, M, M, M, M, 1, M, M, M],\n",
    "       [M, 1, M, M, M, M, M, M, M, M, M, M, 1, M, M],\n",
    "       [M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M],\n",
    "       [M, 1, M, M, M, M, M, M, M, M, M, M, M, M, 1],\n",
    "       [M, M, M, M, M, M, M, M, M, M, M, M, M, M, M]]\n",
    "\n",
    "  #\n",
    "  # variables\n",
    "  #\n",
    "\n",
    "  # requirements (right hand statement)\n",
    "  rhs = [solver.IntVar(-1, 1, 'rhs[%i]' % i) for i in range(n)]\n",
    "\n",
    "  x = {}\n",
    "  for i in range(n):\n",
    "    for j in range(n):\n",
    "      x[i, j] = solver.IntVar(0, 1, 'x[%i,%i]' % (i, j))\n",
    "\n",
    "  out_flow = [solver.IntVar(0, 1, 'out_flow[%i]' % i) for i in range(n)]\n",
    "  in_flow = [solver.IntVar(0, 1, 'in_flow[%i]' % i) for i in range(n)]\n",
    "\n",
    "  # length of path, to be minimized\n",
    "  z = solver.Sum(\n",
    "      [d[i][j] * x[i, j] for i in range(n) for j in range(n) if d[i][j] < M])\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "\n",
    "  for i in range(n):\n",
    "    if i == start:\n",
    "      solver.Add(rhs[i] == 1)\n",
    "    elif i == end:\n",
    "      solver.Add(rhs[i] == -1)\n",
    "    else:\n",
    "      solver.Add(rhs[i] == 0)\n",
    "\n",
    "  # outflow constraint\n",
    "  for i in range(n):\n",
    "    solver.Add(\n",
    "        out_flow[i] == solver.Sum([x[i, j] for j in range(n) if d[i][j] < M]))\n",
    "\n",
    "  # inflow constraint\n",
    "  for j in range(n):\n",
    "    solver.Add(\n",
    "        in_flow[j] == solver.Sum([x[i, j] for i in range(n) if d[i][j] < M]))\n",
    "\n",
    "  # inflow = outflow\n",
    "  for i in range(n):\n",
    "    solver.Add(out_flow[i] - in_flow[i] == rhs[i])\n",
    "\n",
    "  # objective\n",
    "  objective = solver.Minimize(z)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solver.Solve()\n",
    "\n",
    "  print()\n",
    "  print('z: ', int(solver.Objective().Value()))\n",
    "\n",
    "  t = start\n",
    "  while t != end:\n",
    "    print(nodes[t], '->', end=' ')\n",
    "    for j in range(n):\n",
    "      if x[t, j].SolutionValue() == 1:\n",
    "        print(nodes[j])\n",
    "        t = j\n",
    "        break\n",
    "\n",
    "  print()\n",
    "  print('walltime  :', solver.WallTime(), 'ms')\n",
    "  if sol == 'CBC':\n",
    "    print('iterations:', solver.Iterations())\n",
    "\n",
    "\n",
    "\n",
    "sol = 'CBC'\n",
    "if len(sys.argv) > 1:\n",
    "  sol = sys.argv[1]\n",
    "  if sol != 'GLPK' and sol != 'CBC':\n",
    "    print('Solver must be either GLPK or CBC')\n",
    "    sys.exit(1)\n",
    "\n",
    "main(sol)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
